<?php

namespace app\admin\model;


/**
 * 资讯模型
 * Class Information
 * @package app\admin\model
 */
class Information extends Base
{
    protected $autoWriteTimestamp = true;//开启自动添加时间戳
    protected $auto = ["substation_name"];
    protected $insert = ['create_id','create_nick_name'];//自动添加

    //添加人id
    protected function setCreateIdAttr()
    {
        return $this->admin["admin_id"];
    }

    //添加昵称
    protected function setCreateNickNameAttr()
    {
        return $this->admin["nick_name"];
    }

    //auto，分站名称
    protected function setSubstationNameAttr($value,$data)
    {
        $Substation = new Substation();//分站类
        return $Substation->getFieldValue("substation_id", $data["substation_id"], "substation_name");
    }

    //关键字获取器
    public function getKeywordIdAttr($value){
        return StrToArray($value);
    }

    //修改器
    public function setKeywordIdAttr($value)
    {
        if(!empty($value)) {
            $value = explode(",", $value);
            $value = ArrayToStr($value);

        }
        return $value;
    }

    //资讯列表
    public function informationList($keyword = ""){
        return $this
            ->where(function ($q)use ($keyword){
                //模糊查找，标题、分类名称、资讯关键字
                if(!empty($keyword)){
                    $q->whereOr("information_title","like","%".$keyword."%")
                        ->whereOr("classification_name","like","%".$keyword."%")
                        ->whereOr("keywords","like","%".$keyword."%");
                }
            })
            ->where(config("TableDeleteField"), 0)//获取没有删除的
            ->where(function ($q){
                if(!empty($this->substations)){//是否设置了当前网站
                    $q->where("substation_id",$this->substations);
                }else{
                    if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
                        $arr = [];
                        $Substation = new Substation();//分站类
                        $userList = $Substation->userList();//获取分站集合
                        foreach ($userList as $val){
                            $arr[] = $val['substation_id'];
                        }
                        $q->where("substation_id","in",$arr);
                    }
                }
            })
            ->page($this->after_page)
            ->limit($this->after_limit)
            ->orderRaw("substation_id ASC,if(is_display='1',1,0) DESC,if(is_top='1',1,0) DESC,update_time DESC")//排序根据置顶和更新时间降序排列
            ->select();
    }

    //资讯列表总数
    public function informationListCount($keyword = ""){
        return $this
            ->where(config("TableDeleteField"), 0)//获取没有删除的
            ->where(function ($q){
                if(!empty($this->substations)){//是否设置了当前网站
                    $q->where("substation_id",$this->substations);
                }else{
                    if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
                        $arr = [];
                        $Substation = new Substation();//分站类
                        $userList = $Substation->userList();//获取分站集合
                        foreach ($userList as $val){
                            $arr[] = $val['substation_id'];
                        }
                        $q->where("substation_id","in",$arr);
                    }
                }
            })
            ->where(function ($q)use ($keyword){
                //模糊查找，标题、分类名称、资讯关键字
                if(!empty($keyword)){
                    $q->whereOr("information_title","like","%".$keyword."%")
                        ->whereOr("classification_name","like","%".$keyword."%")
                        ->whereOr("keywords","like","%".$keyword."%");
                }
            })
            ->count();
    }

    //验证名称是否正确
    public function checkName($information_title,$information_id,$substation_id){
        return $this
            ->where("information_title",$information_title)
            ->where("information_id","neq",$information_id)
            ->where(function ($q)use ($substation_id){
                if(!empty($substation_id)) {
                    $q->where("substation_id",$substation_id);
                }
            })
            ->where(config("TableDeleteField"),0)//获取没有删除的
            ->count();
    }

    //验证是否有权限操作该分类
    public function checkSave($classification_id,$substation_id){
        $is = [false,false];//验证标识
        if($this->admin["group_id"] != config("SuperAdminGroupId")) {//是否超级管理员
            $Substation = new Substation();//分站类
            $userList = $Substation->userList();//获取分站集合
            $drawing_substation_id = $this->where("classification_id",$classification_id)->value("substation_id");//获取原分站ID
            foreach ($userList as $val){//遍历分站集合
                if(!empty($case_id)) {//修改验证
                    if ($drawing_substation_id == $val["substation_id"]) {
                        $is[0] = true;
                    }
                }else{
                    //添加不验证
                    $is[0] = true;
                }
                if(!empty($substation_id)) {//添加修改验证
                    if ($substation_id == $val["substation_id"]) {
                        $is[1] = true;
                    }
                }else{
                    //删除不验证
                    $is[1] = true;
                }
            }
        }else{
            $is = [true,true];
        }
        return $is[0] === true && $is[1] === true ? true : false;
    }


    //获取关键列表
    public function keywordsList($arr = []){
        $where = "";
        foreach ($arr as $val){//遍历分站集合
            $where .= " information_title like '%".$val."%' or";
        }
        $where = rtrim($where,"or");
        return $this->field("keywords")->where("keywords","NOT NULL")->where($where)->select();
    }

    //设置资讯分类
    public function setInformationClassification($ids,$data){
        unset($data["ids"]);
        return $this
            ->where("information_id","in",$ids)
            ->update($data);
    }
}